const Router = require('koa-router')
const md5 = require('crypto-js/md5')
const Order = require('../dbs/models/order')
const Cart = require('../dbs/models/cart')

const router = new Router({ prefix: '/order' })

router.post('/createOrder', async (ctx) => {
  const { id: cartNo, price, count } = ctx.request.body
  const time = Date()
  const orderID = md5(Math.random() * 1000 + time).toString()
  if (!ctx.isAuthenticated()) {
    ctx.body = {
      code: -1,
      msg: '请登录'
    }
  } else {
    try {
      const findCart = await Cart.findOne({ cartNo })
      if (findCart) {
        const order = new Order({
          id: orderID,
          count,
          total: price * count,
          time,
          user: ctx.session.passport.user,
          name: findCart.detail[0].name,
          imgs: findCart.detail[0].imgs,
          status: 0 // 代付款
        })

        const result = await order.save()
        if (result) {
          await findCart.remove()
          ctx.body = {
            code: 0,
            id: orderID
          }
        } else {
          ctx.body = {
            code: -1,
            msg: 'server error'
          }
        }
      } else {
        ctx.body = {
          code: -1,
          msg: 'server error'
        }
      }
    } catch (e) {
      ctx.body = {
        code: -1,
        msg: e
      }
    }
  }
})

router.post('/getOrders', async (ctx) => {
  if (!ctx.isAuthenticated()) {
    ctx.body = {
      code: -1,
      list: [],
      msg: '请登录'
    }
  } else {
    try {
      const result = await Order.find({
        user: ctx.session.passport.user
      })
      if (result) {
        ctx.body = {
          code: 0,
          list: result
        }
      } else {
        ctx.body = {
          code: -1,
          list: [],
          msg: 'server error'
        }
      }
    } catch (e) {
      ctx.body = {
        code: -1,
        list: [],
        msg: e
      }
    }
  }
})

router.post('/delete', async (ctx) => {
  const { id } = ctx.request.body
  if (!ctx.isAuthenticated()) {
    ctx.body = {
      code: -1,
      list: [],
      msg: '请登录'
    }
  } else {
    try {
      const result = await Order.remove({
        id
      })

      if (result) {
        ctx.body = {
          code: 0,
          result: result
        }
      } else {
        ctx.body = {
          code: -1,
          msg: 'server error'
        }
      }
    } catch (e) {
      ctx.body = {
        code: -1,
        msg: e
      }
    }
  }
})

router.post('/pay', async (ctx) => {
  if (!ctx.isAuthenticated()) {
    ctx.body = {
      code: -1,
      list: [],
      msg: '请登录'
    }
  } else {
    try {
      const result = await Order.update({
        user: ctx.session.passport.user,
        status: 0
      }, {
        status: 1
      })

      if (result) {
        ctx.body = {
          code: 0,
          result: result
        }
      } else {
        ctx.body = {
          code: -1,
          msg: 'server error'
        }
      }
    } catch (e) {
      ctx.body = {
        code: -1,
        msg: e
      }
    }
  }
})

// export default router
module.exports = router
